/**
 * 测试用例获取接口测试用例
 *
 * Created by raquelmao on 2020/06/28.
 */
const chai = require('chai');
const should = chai.should(); // eslint-disable-line no-unused-vars
chai.use(require('chai-http'));

const common = require('./../../common');
const Administrator = require('./../../../models/administrator');
const uri = require('./../uri');
const benchmarks = [
  require('./../../../benchmark/generate-tests/self_designed'),
];

module.exports = describe('/GET tasks.json', function () {
  const administrator = new Administrator();
  let project = {};
  const newUserIds = [];
  const url = '/tasks.json';

  before(function (done) {
    administrator.login()
      .then(() => administrator.createRandomTester())
      .then((tester) => {
        newUserIds.push(tester.userId);
        return tester.createRandomProject();
      })
      .then((res) => {
        project = res;
        done();
      })
      .catch(done);
  });
  after(function (done) {
    administrator.removeUsers(newUserIds)
      .then(() => {
        done();
      })
      .catch(done);
  });
  function getTester() {
    return project.creator;
  }

  common.should.be.user(uri, url, 'get');
  common.should.be.tester(uri, url, 'get');
  const sortByKeys = ['type', 'projectName'];
  common.should.complete.filter(uri, url, 'get', getTester, sortByKeys);

  // 正常发起请求
  benchmarks.forEach((benchmark) => {
    it('it should get tasks', function (done) {
      let projectVersion;
      project.createRandomVersion()
        .then((res) => {
          projectVersion = res;
          return projectVersion.getAllTests(benchmark);
        })
        .then(() => project.creator.login())
        .then(() => new Promise((resolve) => {
          chai.request(uri)
            .get(url)
            .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
            .end((err, res) => {
              res.should.have.status(200);
              resolve();
            });
        }))
        .then(() => new Promise((resolve) => {
          chai.request(uri)
            .get(url)
            .query({ sortBy: 'type', order: 'asc' })
            .set('Authorization', `Bearer ${project.creator.token.accessToken}`)
            .end((err, res) => {
              res.should.have.status(200);
              common.should.be.tasksMessage(res.body);
              resolve();
            });
        }))
        .then(done)
        .catch(done);
    });
  });
});
